package com.hakim.wfb.web.controller.user;

import com.hakim.wfb.customer.service.UserService;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.session.SessionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by walkman on 2017/7/1.
 */

@RestController
@RequestMapping(value = "/user", name = "用户")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

//    @RequestMapping(value = "/loginUser", name = "用户登录", method = RequestMethod.POST)
//    public ObjectResultEx<LoginUserView> loginUser(LoginUserParameter param, HttpServletResponse response) {
//
//        ObjectResultEx<LoginUserView> result = userService.loginUser(param);
//        if (result.isFailed()) {
//            return result;
//        }
//        LoginUserView userView = result.getDataObject();
//        SessionIdentity sessionIdentity = new SessionIdentity();
//        sessionIdentity.setPhone(userView.getUserPhone());
//        sessionIdentity.setUuid(userView.getTid());
//        sessionIdentity.setName(userView.getUserCode());
//        sessionIdentity.setTerminalType(param.getTerminalType().toString());
//        SessionHandler sessionHandler = new SessionHandler();
//        String accessToken = sessionHandler.login(sessionIdentity, response, 7 * 24 * 60 * 60 * 1000l);
//        //保存日志
//        LoginUserLogParameter parameter = new LoginUserLogParameter();
//        BeanUtils.copyProperties(param, parameter);
//        parameter.setAccessToken(accessToken);
//        userService.loginUserLog(parameter);
//        LoginUserView view = new LoginUserView();
//        view.setAccessToken(accessToken);
//        result.setDataObject(view);
//        return result.makeSuccessResult();
//    }

//    @RequestMapping(value = "/logoutUser", name = "用户登出", method = RequestMethod.POST)
//    public ResultEx logoutUser(LogoutUserParameter param, HttpServletRequest request, HttpServletResponse response) {
//        param.setAccessToken(SessionHandler.getAccessToken(request));
//        SessionHandler sessionHandler = new SessionHandler();
//        sessionHandler.logout(request, response);
//        return userService.logoutUser(param);
//    }

//    @RequestMapping(value = "/islogin", name = "是否已登录", method = RequestMethod.POST)
//    public ObjectResultEx<AccountLoginStatusView> isLogin(HttpServletRequest request) {
//        ObjectResultEx<AccountLoginStatusView> resultEx = new ObjectResultEx<>();
//        AccountLoginStatusView view = new AccountLoginStatusView();
//        String accessToken = SessionHandler.getAccessToken(request);
//        SessionHandler<SessionIdentity> sessionHandler = new SessionHandler<>();
//        SessionIdentity sessionIdentity = sessionHandler.getSessionIdentity(accessToken, SessionIdentity.class);
//        if (ObjectUtils.isEmpty(sessionIdentity)) {
//            view.setStatus(0);
//            resultEx.setDataObject(view);
//            return resultEx.makeSuccessResult();
//        }
//        view.setStatus(1);
//        resultEx.setDataObject(view);
//        return resultEx.makeSuccessResult();
//    }

//    @RequestMapping(value = "/registerUser", name = "注册用户", method = RequestMethod.POST)
//    public ResultEx registerUser(RegisterUserParameter param) {
//
//        return userService.registerUser(param);
//    }
//
//    @RequestMapping(value = "/registerAndloginUser", name = "用户注册登录", method = RequestMethod.POST)
//    public ObjectResultEx<LoginUserView> registerAndloginUser(RegisterAndLoginUserParameter param, HttpServletResponse response) {
//        ObjectResultEx<LoginUserView> result = new ObjectResultEx<>();
//        try {
//            RegisterUserParameter registParam = new RegisterUserParameter();
//            BeanUtils.copyProperties(param, registParam);
//            ResultEx resultEx = userService.registerUser(registParam);
//            if (resultEx.isFailed()) {
//                return result.makeResult(resultEx.retCode, resultEx.getRetMsg());
//            }
//            LoginUserParameter loginParam = new LoginUserParameter();
//            loginParam.setUserCode(param.getUserCode());
//            loginParam.setPassword(param.getUserPassword());
//            loginParam.setClientIp(param.getClientIp());
//            loginParam.setTerminalType(param.getTerminalType());
//            return this.loginUser(loginParam, response);
//        } catch (Exception e) {
//            logger.error("", e);
//            return result.makeFailedResult("注册失败");
//        }
//    }


}
